package com.example.administrator.myweiyue.ui.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.myweiyue.MyApplication;
import com.example.administrator.myweiyue.R;
import com.example.administrator.myweiyue.utils.DialogHelper;
import com.trello.rxlifecycle2.LifecycleTransformer;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.swipebacklayout.BGASwipeBackHelper;


/**
 * Created by Administrator on 2018-01-23.
 */
public abstract class BaseActivity<T extends BaseContract.BasePresenter> extends MySupportActivity implements IBase, BaseContract.BaseView, BGASwipeBackHelper.Delegate {
    protected Unbinder mUnbinder;
//    protected AppApplication mApplication;
//    protected IBus ibus;

//    @Nullable
//    @BindView(R.id.SimpleMultiStateView)
//    SimpleMultiStateView mSimpleMultiStateView;

    @Nullable
    @Inject
    T mPresenter;

    private BGASwipeBackHelper mSwipeBackHelper;
    private View mRootView;
    private AlertDialog mLoadingDialog;
    protected Context context;

    @Override
    protected final void onCreate(@Nullable Bundle savedInstanceState) {
        // 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackManager.getInstance().init(this) 来初始化滑动返回」
        // 在 super.onCreate(savedInstanceState) 之前调用该方法
        initSwipeBackFinish();
        super.onCreate(savedInstanceState);
        if (getLayoutId() > 0) {
            //之后就可以设置title了
            mRootView = getLayoutInflater().inflate(getLayoutId(), null);
            setContentView(mRootView);
            context=this;
            mUnbinder = ButterKnife.bind(this);

        }
        //注入dagger2
        initInjector(MyApplication.getAppComponent());
        //jiangView传给Present
        attachView();
        //初始化View
        afterViewCreate(mRootView);
//        初始化Title

        if (isShowTitle()) {
            initTitle();
        }
        initStateView();
        initData();
        mLoadingDialog = DialogHelper.getLoadingDialog(this);

    }

    private void initStateView() {
//        if (mSimpleMultiStateView == null) return;
//        mSimpleMultiStateView.setEmptyResource(R.layout.view_empty)
//                .setRetryResource(R.layout.view_retry)
//                .setLoadingResource(R.layout.view_loading)
//                .setNoNetResource(R.layout.view_nonet)
//                .build()
//                .setonReLoadlistener(new MultiStateView.onReLoadlistener() {
//                    @Override
//                    public void onReload() {
                     onRetry();
//                    }
//                });
    }

    private void attachView() {
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
    }

    @Override
    public View getView() {
        return mRootView;
    }

    private void initSwipeBackFinish() {
        mSwipeBackHelper = new BGASwipeBackHelper(this, this);
        // 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackManager.getInstance().init(this) 来初始化滑动返回」
        // 下面几项可以不配置，这里只是为了讲述接口用法。
        // 设置滑动返回是否可用。默认值为 true
        mSwipeBackHelper.setSwipeBackEnable(true);
        // 设置是否仅仅跟踪左侧边缘的滑动返回。默认值为 true
        mSwipeBackHelper.setIsOnlyTrackingLeftEdge(false);
        // 设置是否是微信滑动返回样式。默认值为 true
        mSwipeBackHelper.setIsWeChatStyle(true);
        // 设置阴影资源 id。默认值为 R.drawable.bga_sbl_shadow
        mSwipeBackHelper.setShadowResId(R.drawable.bga_sbl_shadow);
        // 设置是否显示滑动返回的阴影效果。默认值为 true
        mSwipeBackHelper.setIsNeedShowShadow(true);
        // 设置阴影区域的透明度是否根据滑动的距离渐变。默认值为 true
        mSwipeBackHelper.setIsShadowAlphaGradient(true);
        // 设置触发释放后自动滑动返回的阈值，默认值为 0.3f
        mSwipeBackHelper.setSwipeBackThreshold(0.3f);
    }


    protected void initTitle() {
//隐藏啊设置啊等等

    }

    protected boolean isShowTitle() {
        return false;
    }

    protected void showLoadingDialog() {
        if (mLoadingDialog != null)
            mLoadingDialog.show();
    }

    protected void showLoadingDialog(String str) {
        if (mLoadingDialog != null) {
            TextView tv = (TextView) mLoadingDialog.findViewById(R.id.tv_load_dialog);
            tv.setText(str);
            mLoadingDialog.show();
        }
    }

    protected void hideLoadingDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != Unbinder.EMPTY) {
            mUnbinder.unbind();
        }
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }


    @Override
    public void showLoading() {
//        if (mSimpleMultiStateView != null) {
//            mSimpleMultiStateView.showLoadingView();
//        }
    }

    @Override
    public void showSuccess() {
//        if (mSimpleMultiStateView != null) {
//            mSimpleMultiStateView.showContent();
//        }
    }

    @Override
    public void showFaild() {
//        if (mSimpleMultiStateView != null) {
//            mSimpleMultiStateView.showErrorView();
//        }
    }

    @Override
    public void showNoNet() {
//        if (mSimpleMultiStateView != null) {
//            mSimpleMultiStateView.showNoNetView();
//        }
    }

    @Override
    public <T1> LifecycleTransformer<T1> bindToLife() {
        return this.<T1>bindToLifecycle();
    }

    /**
     * 是否支持滑动返回。这里在父类中默认返回 true 来支持滑动返回，如果某个界面不想支持滑动返回则重写该方法返回 false 即可
     *
     * @return
     */
    @Override
    public boolean isSupportSwipeBack() {
        return true;
    }

    /**
     * 正在滑动返回
     *
     * @param slideOffset 从 0 到 1
     */
    @Override
    public void onSwipeBackLayoutSlide(float slideOffset) {
    }

    /**
     * 没达到滑动返回的阈值，取消滑动返回动作，回到默认状态
     */
    @Override
    public void onSwipeBackLayoutCancel() {
    }

    /**
     * 滑动返回执行完毕，销毁当前 Activity
     */
    @Override
    public void onSwipeBackLayoutExecuted() {
        mSwipeBackHelper.swipeBackward();
    }

    public boolean useEventBus() {
        return false;
    }

    /**
     * 根据传入的class进行activity的跳转
     *
     * @param c
     */
    public void jumpActivity(Class<? extends Activity> c) {
        if (c != null) {
            startActivity(new Intent(this, c));
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (useEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onStop() {
        if (useEventBus()) {
            EventBus.getDefault().unregister(this);
        }

        super.onStop();
    }


}
